<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true" v-if="permission.receiving_export">导出</el-button>
      </template>
    </avue-crud>
    <export-comp ref="exportComp" page-api-url="mes-warehouse/receiving/page" :option="option" :page="page" :query="query" file-name="收料记录"> </export-comp>
  </basic-container>
</template>

<script>
import { getList } from '@/api/warehouse/receiving';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp/index';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
export default {
  components: {
    exportComp,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 406px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 4,
        searchMenuPosition: 'right',
        border: true,
        index: true,
        indexLabel: '序号',
        selection: true,
        dialogClickModal: false,
        searchLabelWidth: 100,
        searchSpan: 6,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        menu: false,
        menuWidth: 100,
        columnBtn: false,
        column: [
          {
            label: '单据号',
            prop: 'sheetNo',
            width: 180,
            search: true,
            searchOrder: 2
          },
          {
            label: '行号',
            prop: 'purchaseOrderLot'
          },
          {
            label: '供应商',
            prop: 'vendorName',
            search: true,
            searchOrder: 6
          },
          {
            label: '品牌',
            prop: 'partBrand'
          },
          {
            label: '物料编码',
            prop: 'partNo',
            search: true,
            searchOrder: 3
          },
          {
            label: '物料型号',
            prop: 'partModel',
            search: true,
            searchOrder: 4
          },
          {
            label: '接受数量',
            prop: 'receiveQuantity'
          },
          {
            label: '单位',
            prop: 'uom'
          },
          {
            label: '收料人',
            prop: 'receiveByName'
          },
          {
            label: '收料时间',
            width: 130,
            prop: 'receiveTime',
            search: true,
            searchOrder: 1,
            type: 'date',
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期'
          },
          {
            label: '供应商批次',
            prop: 'vendorLotNo'
          },
          {
            label: '送货单号',
            prop: 'asnNo',
            width: 180
          },
          {
            label: '收料类型',
            prop: 'receiveType',
            width: 150,
            type: 'select',
            search: true,
            searchOrder: 5,
            dicUrl: '/api/blade-system/dict/dictionary?code=wh_receiving_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '备注',
            prop: 'remark'
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.receiving_view, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  created() {},
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.receiveTime && params.receiveTime.length > 0) {
        params.queryStartTime = params.receiveTime[0];
        params.queryEndTime = params.receiveTime[1];
        delete params.receiveTime;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  }
};
</script>

<style lang="scss" scoped>
.dialog-content {
  height: 70vh;
  overflow-y: scroll;
}
.lookPackDialog {
  ::v-deep.avue-crud__menu {
    display: none;
  }
}
::v-deep .el-dialog__header {
  padding: 16px 24px;
  min-height: 20px;
  border-bottom: 1px solid #f0f0f0;
}
</style>
